Cannot read values from [NSUserDefaults standardUserDefaults] after synchronize

Posted by Nonlinearsound on Stack Overflow See other posts from Stack Overflow or by Nonlinearsound
Published on 2010-04-29T13:21:48Z Indexed on 2010/04/29 13:27 UTC
Read the original article Hit count: 394

In the Application Delegate didFinishLaunching method, I am using the following code to build up a new NSDictionary to be used as the new settings bundle for the user:

NSNumber *testValue = (NSNumber*)[[NSUserDefaults standardUserDefaults] objectForKey:@"settingsversion"];
if (testValue == nil)
{
    NSNumber *numNewDB = [NSNumber numberWithBool:NO];
    NSNumber *numFirstUse = [NSNumber numberWithBool:YES];
    NSDate *dateLastStatic = [NSDate date];
    NSDate *dateLastMobile = [NSDate date];
    NSNumber *numSettingsversion = [NSNumber numberWithFloat:1.0];

    NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
                                 numNewDB, @"newdb",
                                 numFirstUse, @"firstuse",
                                 numSettingsversion, @"settingsversion",
                                 dateLastStatic, @"laststaticupdate",
                                 dateLastMobile, @"lastmobileupdate",
                                 nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

Later in another ViewController I am trying to read back a value from that same Dictionary, saved as the NSUserDefaults - well at least I thought it would, but I don't get any valid object pointer for the desired member lastUpdate back there:

in .h file:

NSDate *lastUpdate;

in the .m file in a member function:

lastUpdate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"laststaticupdate"];

Even, if I print out the content of [NSUserDefaults standardUserDefaults] I only get this:

2010-04-29 15:13:22.322 myApp[4136:207] Content of UserDefaults: <NSUserDefaults: 0x11d340>

This leads me to the conclusion that there is no standardUserDefaults dictionary somewhere in memory or it cannot be determined as such a structure.

Edit: Every time, I restart the app ión the device, the check for testValue is Nil and I am building up the dictionary again but after one run it should be persistent on the Phone, right?

Am I doing something wrong somewhere in between? I have the feeling that I yet didn't really understand how to load and save settings persistent for a certain application on the iPhone.

Is there anything I have to do additionally to this? Integrating a settings.bundle in XCode or saving the dictionary manually to the Documents folder?

Can someone help me out here? Thanks a lot!

© Stack Overflow or respective owner

Related posts about nsuserdefaults

Related posts about iphone